<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入 layui.css -->
<%--    <link href="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/css/layui.css" rel="stylesheet">--%>

    <!-- 引入 layui.js -->
<%--    <script src="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/layui.js"></script>--%>

    <link href="${pageContext.request.contextPath}/statics/layui/css/layui.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/statics/layui/layui.js"></script>

    <script src="${pageContext.request.contextPath}/statics/jquery-1.8.3.min.js"></script>
</head>
<body>
<a href="add.jsp">新增</a>
<div>
    <table class="layui-table">
        <tr>
            <td>类别</td>
            <td>品种</td>
            <td>产地</td>
            <td>总重量(公斤)</td>
            <td>单价(元/公斤)</td>
            <td>操作</td>
        </tr>

        <c:forEach items="${requestScope.list}" var="list">
            <tr>
                <td>${list.type}</td>
                <td>${list.breed}</td>
                <td>${list.area}</td>
                <td>${list.weight}</td>
                <td>${list.price}</td>
                <td>
                    <a href="javascript:view(${list.fruitId})">查看</a>
                    <a href="fruit/${list.fruitId}">详情</a>
                    <a href="javascript:void(0);" onclick="deleteFruit(${list.fruitId})">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</div>

<script>
    function deleteFruit(id) {
        let c = confirm('确定要删除吗？');
        if (c){

            // $.get('delete','id='+id,function (data) {
            //     // data 为 success
            //     if (data=='success'){
            //         location.reload();
            //     }else {
            //         alert('删除失败')
            //     }
            // })

            $.ajax({
                url:'fruit/'+id,
                type:'delete',
                dataType:"text",
                success:function(data){
                    // data 为 success
                    if (data=='success'){
                        alert('删除成功')
                        location.reload();
                    }else {
                        alert('删除失败')
                    }
                }
            })
        }
    }

    function view(id) {
        $.get('view/'+id,function (data) {
            console.log(data)
        },'json');
    }
</script>
</body>
</html>
